package com.zhidisoft.blog.admin.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LoginInterceptor extends HandlerInterceptorAdapter {

	/*@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String userId = (String) request.getSession().getAttribute("userId");
		if (StringUtils.isEmpty(userId)) {
			String ctxPath = request.getContextPath();
			response.sendRedirect(ctxPath + "/login");
			
			return true;
		}
		return super.preHandle(request, response, handler);
	}*/
	
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler) throws Exception{
		System.out.println(request.getContextPath());// 项目路径(如果项目部署在tomcat ROOT
		// 目录下,就是/)
		System.out.println(request.getServletPath());// 访问路径()
		
		String servletPath = request.getServletPath();//获取访问路径
		
		System.out.println(servletPath);
		//获取用户的id
		String userId = (String) request.getSession().getAttribute("userId");
		if (StringUtils.isEmpty(userId)) {
			//获取项目路径
			String ctxPath = request.getContextPath();
			response.sendRedirect(ctxPath + "/login?servletPath=" + servletPath);
			return false;
		}
		return super.preHandle(request, response, handler);
		
	}
}
